package com.ypx;

import java.util.HashMap;
import java.util.Map;

public class MapPractice {
    public static void main(String[] args) {
        Map<String, String> data = new HashMap<>();
        data.put("小王", "A");
        data.put("小李", "B");
        data.put("小张", "B");
        data.put("小李1", "A");
        data.put("小周", "D");
        data.put("小吴", "A");
        data.put("小郑", "D");
        data.put("小陈", "A");
        data.put("小孙", "B");
        data.put("小明", "A");
        data.put("小明2", "C");
        data.put("小钱", "C");
        data.put("小郑1", "A");
        data.put("小郑2", "B");
        data.put("小郑3", "D");
        data.put("小郑4", "D");
        data.put("小郑5", "B");

        count(data);
    }
    public static void count(Map<String, String> data){
        /**
         * 1.创建一个新的Map集合，用来储存统计结果
         * 2.遍历data集合，key---value
         * 3.将value（A）当作k存入新集合
         * 4.如果A存在则将其value+1，不存在则将其value置为1
         * 5.遍历新集合，key---value
         * */
        Map<String, Integer> resultmap = new HashMap<>();
        data.forEach((k,v)->{
           boolean containsKey = resultmap.containsKey(v);
           if(containsKey){
               resultmap.put(v, resultmap.get(v)+1);
           }else{
               resultmap.put(v,1);
           }
        });
        resultmap.forEach((k,v)->{
            System.out.println(k+"出现了"+v+"次");
        });
    }
}
